package com.bjpowernode.boss.mvc.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.bjpowernode.boss.constants.KeyConst;
import com.bjpowernode.boss.mvc.model.dto.OperatorLoginDto;
import com.bjpowernode.boss.mvc.model.dto.PmsMenuDto;
import com.bjpowernode.boss.mvc.model.query.OperatorLoginQuery;
import com.bjpowernode.boss.mvc.model.result.AjaxResult;
import com.bjpowernode.boss.mvc.service.OperatorService;
import com.bjpowernode.boss.utils.RedisUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 操作员控制类
 */
@RestController
@Slf4j
public class OperatorController {

    @Resource
    private OperatorService operatorService;

    @Resource
    private RedisUtil redisUtil;

    @PostMapping("/operator/login")
    public AjaxResult operatorLogin(@RequestBody @Validated OperatorLoginQuery operatorLoginQuery) {
        log.info("接收到的登录数据位operatorLoginQuery : {}", operatorLoginQuery);
        // 操作员登录功能
        OperatorLoginDto operatorLoginDto = operatorService.operatorLogin(operatorLoginQuery);
        // 返回登录数据
        return AjaxResult.ok(operatorLoginDto);
    }

    @GetMapping("/operator/menus")
    public AjaxResult loadMenus(@RequestHeader("operatorId") Long operatorId) {
        log.info("要 获取动态菜单得指定得操作员id : {}", operatorId);
        // 根据 操作员id , 查询redis , 判断redis中是否存在该数据
        String key = KeyConst.OPERATOR_MENUS + operatorId;

        List<PmsMenuDto> menus = redisUtil.getData(
                () ->
                {
                    // 获取json数据
                    String redisJsonStr = redisUtil.getValue(key);
                    // 解析JSON数组字符串
                    JSONArray jsonArray = JSONUtil.parseArray(redisJsonStr);
                    // 假设 PmsMenuDto 是你的目标Java类
                    List<PmsMenuDto> menuDtoList = jsonArray.toList(PmsMenuDto.class);
                    return menuDtoList;
                },
                () ->
                {
                    return operatorService.loadMenusByOperatorId(operatorId);
                },
                (t) ->
                {
                    // 并将查询到的数据存入到 redis中
                    String jsonStr = JSONUtil.toJsonStr(t);
                    redisUtil.setValue(key, jsonStr, 5, TimeUnit.MINUTES);
                });


        /*String redisJsonToken = redisUtil.getValue(key);
        // 如果存在 , 直接 返回
        List<PmsMenuDto> menus = null;
        if (StrUtil.isNotBlank(redisJsonToken)) {
            // 解析 json
            // 解析JSON数组字符串
            JSONArray jsonArray = JSONUtil.parseArray(redisJsonToken);
            // 假设 PmsMenuDto 是你的目标Java类
            menus = jsonArray.toList(PmsMenuDto.class);
        }
        if (menus == null) {
            // 不存在 , 去数据库中查询
            menus = operatorService.loadMenusByOperatorId(operatorId);
            // 并将查询到的数据存入到 redis中
            String jsonStr = JSONUtil.toJsonStr(menus);
            redisUtil.setValue(key, jsonStr);
        }*/
        return AjaxResult.ok(menus);
    }

    /**
     * 退出功能
     */
    @GetMapping("/operator/logout")
    public AjaxResult logout(@RequestHeader(value = "operatorId") Integer operatorId) {
        log.info("程序退出............");
        operatorService.logout(operatorId);
        return AjaxResult.ok();
    }

}
